package com.milo.thread.pool;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 演示有返回值的线程池用法
 * @author Milo Lee
 * @date 2021-1-28 16:33
 *
 */
public class ThreadPoolTest {

    public static void main(String[] args) {

        //创建大小为5个的线程池
        ExecutorService executorService = Executors.newScheduledThreadPool(5);
        List<Callable<List<Map<String,String>>>> resultLists = new ArrayList<>();
        Callable<List<Map<String,String>>> task = null;
        for(int j = 0 ; j < 10 ; j ++){
            task = () -> {
                List<Map<String,String>> oaoMesses = new ArrayList<Map<String,String>>();
                    for (int i = 0 ; i < 10 ; i ++){
                        oaoMesses.add(null);
                    }
                    return oaoMesses;

            };
            resultLists.add(task);
        }
        try {
            //任务批量提交
            List<Future<List<Map<String,String>>>> retData = executorService.invokeAll(resultLists);
            for (Future<List<Map<String,String>>> future : retData) {
                System.out.println(JSONObject.toJSONString(future.get()));
            }
            executorService.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
